<template>
  <j-modal :title="title" :width="width" :visible="visible" switchFullscreen @cancel="handleCancel">
    <template slot="footer">
      <a-button @click="handleCancel">{{$t('关闭')}}</a-button>
    </template>
    <div>
      <div>
        <span>{{$t('业务时间')}}：{{ model.createTime }}</span>
        <span style="margin-left: 30px">{{$t('仓库')}}：{{ model.storeId_dictText }}</span>
        <span style="margin-left: 30px">{{$t('出入库类型')}}：{{ model.orderType_dictText }}</span>
        <span style="margin-left: 30px">{{$t('业务类型')}}：{{ model.bisType_dictText }}</span>
        <span style="margin-left: 30px">{{$t('操作员')}}：{{ model.createBy }}</span>
        <span style="margin-left: 30px">{{$t('关联业务单号')}}：{{ model.bisOrderId }}</span>
      </div>
      <div>
        <div style="margin-top: 20px; font-weight: bold">{{$t('库存变化明细')}}：</div>
        <div style="margin-top: 20px; ">
          <a-table
            ref="table"
            size="middle"
            bordered
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="false"
            :loading="loading"
          >
          </a-table>
        </div>
      </div>
    </div>
  </j-modal>
</template>

<script>
import { getAction } from '../../../api/manage'
export default {
  name: 'GoodsStockOrderModal',
  components: {},
  data() {
    return {
      title: '',
      width: 1100,
      visible: false,
      disableSubmit: false,
      loading: false,
      model: {},
      columns: [
        {
          title: this.$t('商品编码'),
          align: 'center',
          dataIndex: 'goodsCode',
        },
        {
          title: this.$t('商品名称'),
          align: 'center',
          dataIndex: 'goodsId_dictText',
        },
        {
          title: this.$t('业务前数量'),
          align: 'center',
          dataIndex: 'beforeNum',
          customCell:()=>{
            return{
              style:{
                textAlign:'right'
              }
            }
          }
        },
        {
          title: this.$t('业务前金额'),
          align: 'center',
          dataIndex: 'beforeAmount',
          customCell:()=>{
            return{
              style:{
                textAlign:'right'
              }
            }
          },
          customRender:(text,record)=>{
            return text.toFixed(2)
          }
        },
        {
          title: this.$t('业务数量'),
          align: 'center',
          dataIndex: 'bisNum',
          customCell:()=>{
            return{
              style:{
                textAlign:'right'
              }
            }
          }
        },
        {
          title: this.$t('业务成本'),
          align: 'center',
          dataIndex: 'cost',
          customCell:()=>{
            return{
              style:{
                textAlign:'right'
              }
            }
          },
          customRender:(text,record)=>{
            return text.toFixed(2)
          }
        },
        {
          title: this.$t('业务后数量'),
          align: 'center',
          dataIndex: 'afterNum',
          customCell:()=>{
            return{
              style:{
                textAlign:'right'
              }
            }
          }
        },
        {
          title: this.$t('业务后金额'),
          align: 'center',
          dataIndex: 'afterAmount',
          customCell:()=>{
            return{
              style:{
                textAlign:'right'
              }
            }
          },
          customRender:(text,record)=>{
            return text.toFixed(2)
          }
        },
      ],
      dataSource: [],
    }
  },
  methods: {
    edit(record) {
      console.log(record)
      this.visible = true
      this.model = Object.assign({}, record)
      this.dataSource = []
      this.loading = true
      getAction('/goods/bisGoodsStockOrder/queryBisGoodsStockChangeByMainId', { id: record.id }).then((res) => {
        if (res.success) {
          this.dataSource = res.result
          this.addSumRow(['goodsId_dictText','beforeNum','beforeAmount','bisNum','cost','afterNum','afterAmount'],this.dataSource)
        }
      })
    },
    addSumRow(myArr,dataSource){
      let total = {}
      myArr.forEach((col,index) => {
        if(index == 0){
          total[col] = this.$t('合计')
        }else{
          total[col] = 0
        }
      })
      console.log(total)
      for(let key in total){
        dataSource.forEach(ds=>{
          if (!isNaN(total[key])) {
            total[key] += Number(ds[key])
          }
        })
      }
      dataSource.push(total)
      this.loading = false
    },
    handleOk() {},
    submitCallback() {
      this.$emit('ok')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
  },
}
</script>